#include "animation.h"
void drawAnimatedEntity(GameObject *entity)
  {
    
    if (entity->frameTimer <= 0)
    {
        entity->frameTimer = TIME_BETWEEN_2_FRAMES;

       
        entity->frameNumber++;

        if(entity->frameNumber >= entity->sprite->w / entity->w)
            entity->frameNumber = 0;

    }
    
    else
        entity->frameTimer--;


   
    SDL_Rect dest;

    
    dest.x = (entity->x - map.startX);
    dest.y = (entity->y - map.startY)-150;
    dest.w = entity->w;
    dest.h = entity->h;

    SDL_Rect src;

    src.x = entity->frameNumber * entity->w;
    src.y = 0;
    src.w = entity->w;
    src.h = entity->h;


    SDL_BlitSurface(entity->sprite, &src, jeu.screen, &dest);

}
  


void drawanimatedplayer(GameObject *entity)
{
    if(player.etat == 2)
    {
        player.frameNumber = 0;
        player.frameTimer = TIME_BETWEEN_2_FRAMES;
    }
    else
    {
        if (player.frameTimer <= 0)
        {
            player.frameTimer = TIME_BETWEEN_2_FRAMES;
            player.frameNumber++;
            if(player.frameNumber >= player.sprite->w / PLAYER_WIDTH)
                player.frameNumber = 0;
        }
        else
            player.frameTimer--;
    }    
    drawAnimatedEntity(&player);
}  

 

void changeAnimation(GameObject *entity, char *name)
{
   
    if (entity->sprite != NULL)
    {
        SDL_FreeSurface(entity->sprite);
    }
    
    entity->sprite = loadImage(name);
    entity->frameNumber = 0;
    entity->frameTimer = TIME_BETWEEN_2_FRAMES;
}
